package bo;

import IBO.IIntervention;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import run.PublisherImpl;

public class ListeInterventions extends PublisherImpl{
        private static ListeInterventions instance;
        private int singletonData;
        private Vector<Intervention> _listeIntervention = new Vector<Intervention>();

	//*******************************************************************
	//Constructeurs
	//*******************************************************************

        private ListeInterventions() throws RemoteException{
        }    
    
	//*******************************************************************
	//Méthode Static
	//*******************************************************************
        
        public static ListeInterventions getInstance() throws RemoteException{
                if(instance == null) instance = new ListeInterventions();
                return instance;
        }
        
        //*******************************************************************
	//Setters
	//*******************************************************************
        
        public synchronized void remplirListeIntervention(Vector<Intervention> listeIntervention) throws RemoteException {
                for(int i=0; i<listeIntervention.size(); i++)
                    this.ajouterIntervention(listeIntervention.get(i));
        }
    
        public synchronized void setSingletonData(int singletonData) {
                this.singletonData = singletonData;
        }

	//*******************************************************************
	//Getters
	//*******************************************************************
        
        public synchronized Vector<Intervention> getListeIntervention() throws RemoteException {
                return _listeIntervention;
        }

        public synchronized int getSingletonData() throws RemoteException {
                return singletonData;
        }

        public synchronized int nombreInterventions() throws RemoteException {
            return this._listeIntervention.size();
        }
        
	//*******************************************************************
	//Methode
	//*******************************************************************
        public synchronized Intervention rechercherIntervention(int idIntervention) throws RemoteException {
                for(int i=0 ; i<this._listeIntervention.size() ; i++){
                int id = this._listeIntervention.get(i).getIdIntervention();
                    if (id == idIntervention) {
                        return this._listeIntervention.get(i);
                    }
                }
                 return null;
        }    
    
        public synchronized Vector<Intervention> listeInterventionPlanifiee() throws RemoteException {
                Vector<Intervention> liste = new Vector<Intervention>();
                for(int i=0 ; i<this.getListeIntervention().size() ; i++){
                    if (this.getListeIntervention().get(i).getStatut().getlibelle().compareTo("PL") == 0) 
                    liste.add(this.getListeIntervention().get(i));
                }
                return liste;
        }    

        public synchronized Vector<Intervention> listeInterventionAPlanifier() throws RemoteException {
                Vector<Intervention> liste = new Vector<Intervention>();
                for(int i=0 ; i<this.getListeIntervention().size() ; i++){
                    if (this.getListeIntervention().get(i).getStatut().getlibelle().compareTo("CR") == 0) 
                    liste.add(this.getListeIntervention().get(i));
                }
                return liste;
        }    
    
	public synchronized Vector<Intervention> listeInterventionDeplanifiee() throws RemoteException {
                Vector<Intervention> liste = new Vector<Intervention>();
                for(int i=0 ; i<getListeIntervention().size() ; i++){
                    if (this.getListeIntervention().get(i).getStatut().getlibelle().compareTo("DE") == 0) {
                        liste.add(this.getListeIntervention().get(i));
                    }
                }
                return liste;
        }    
    
	public synchronized Vector<Intervention> listeInterventionEnCours() throws RemoteException{
                Vector<Intervention> liste = new Vector<Intervention>();
                for(int i=0 ; i<getListeIntervention().size() ; i++){
                    if (this.getListeIntervention().get(i).getStatut().toString().contentEquals("EN")) {
                        liste.add(this.getListeIntervention().get(i));
                    }
                }
                return liste;
        }    

	//*******************************************************************
	//Associations
	//******************************************************************* 
        
        public synchronized boolean ajouterIntervention(Intervention intervention) throws RemoteException{
                if(intervention==null)return false;
                if(instance.contientIntervention(intervention))return false;
                if(!instance.peutAjouterIntervention())return false;
                instance._listeIntervention.add(intervention);
                this.notifyAllSubscibers();
                return true;
        }
    
        public boolean supprimerIIntervention(IIntervention intervention) throws RemoteException{
                if(intervention==null)return false;
                Intervention interventionTampon = rechercherIntervention(intervention.getIdIntervention());
                if(!instance.contientIntervention(interventionTampon))return false;
                interventionTampon.supprimerTechnicien();
                interventionTampon.supprimerClient();
                instance._listeIntervention.remove(interventionTampon);
                this.notifyAllSubscibers();
                return true;            
        }
        
        public synchronized boolean supprimerIntervention(Intervention intervention) throws RemoteException{
                if(intervention==null)return false;
                if(!instance.contientIntervention(intervention))return false;
                instance._listeIntervention.remove(intervention);
                intervention.supprimerTechnicien();
                intervention.supprimerClient();
                this.notifyAllSubscibers();
                return true;
        }
    
        public boolean peutAjouterIntervention(){
                return true;
        }
    
        public boolean contientIntervention(Intervention intervention){
                return instance._listeIntervention.contains(intervention);
        }
}